package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.IOrderTimeService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthService;
import cn.wolfcode.p2p.base.util.AjaxResult;
import cn.wolfcode.p2p.base.util.RequireLogin;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class VideoAuthController {

    @Autowired
    private IVideoAuthService videoAuthService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private ILoginInfoService loginInfoService;

    @Autowired
    private IOrderTimeService orderTimeService;

    @RequireLogin
    @RequestMapping("/videoAuditOrder")
    public String videoAuthPage(Model model) {
        UserInfo userInfo = userInfoService.getCurrent();
        //如果当前用户已经拥有视频验证码
        if (userInfo.isHasVideoAuth()) {
            //设置共享
            model.addAttribute("auditing", false);
            return "videoOrder_result";
        } else {
            //已经提交了验证码
            if (userInfo.getVideoAuthId() != null) {
                model.addAttribute("auditing", true);
                model.addAttribute("videoAuth", videoAuthService.get(userInfo.getVideoAuthId()));
                return "videoOrder_result";
            } else {
                //没有申请的 申请页面
                //auditors
                List<LoginInfo> auditorList = loginInfoService.selectIsAuditorList();
                model.addAttribute("auditors", auditorList);
                //orderDates
                List<Date> orderDates = new ArrayList<>();
                Date now = new Date();
                //可以预约距离现在的五天后
                orderDates.add(now);
                orderDates.add(DateUtils.addDays(now, 1));
                orderDates.add(DateUtils.addDays(now, 2));
                orderDates.add(DateUtils.addDays(now, 3));
                orderDates.add(DateUtils.addDays(now, 4));
                model.addAttribute("orderDates", orderDates);
                //orderTimes
                model.addAttribute("orderTimes", orderTimeService.listAll());
                return "videoOrder";
            }
        }
    }

    @RequestMapping("/videoAuth_apply")
    @ResponseBody
    public AjaxResult videoAuthApply(Long timeId, String orderDate,
                                     Long auditorId) {
        AjaxResult result = null;
        try {
            videoAuthService.apply(timeId, orderDate, auditorId);
            result = new AjaxResult("预约成功");
        } catch (DisplayableException e) {
            e.printStackTrace();
            //如果底层是抛出这个异常的话 则把 异常信息显示给用户看
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            //把异常信息写入到日志中
            result = new AjaxResult(false, "系统异常,程序员哥哥正在努力抢救中...");
        }
        return result;
    }
}
